//app.js
var QQMapWX = require('/qqmap/qqmap-wx-jssdk.js');
App({
  onLaunch: function () {
    // 登录
    var that = this
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        var code = res.code
        console.log(res)
        that.getuserInfo(code)
      }
    })
  },
  getuserInfo(code) {
    // 获取用户信息
    var that = this
    wx.getUserInfo({
      success: res => {
        // 可以将 res 发送给后台解码出 unionId
        var userInfo=  res.userInfo
        wx.request({
          url: 'https://www.hohoo.tech/index.php/Api/Index/getopenid?code=' + code + '&wx_pic=' + userInfo.avatarUrl + '&wxname=' + userInfo.nickName + '&sex=' + userInfo.gender,
          success:res =>{
            console.log(res)
            if (res.errMsg =='request:ok'){
              wx.setStorageSync('openId', res.data)
            }
            that.globalData.openId = res.data
          }
        })
        this.globalData.userInfo = res.userInfo
          wx.setStorage({
            key: 'userInfo',
            data: res.userInfo,
          })
        // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
        // 所以此处加入 callback 以防止这种情况
        if (this.userInfoReadyCallback) {
          this.userInfoReadyCallback(res)
        }
      }
    })
  },
  getLocation(home) {
    var that = this
    wx.getLocation({
      success: function (res) {
        console.log(res)
        // 调用接口
        // 实例化API核心类
        var demo = new QQMapWX({
          key: 'B4YBZ-IS33V-3NWPV-URRKY-S7262-HSBD6' // 必填
        });
        demo.reverseGeocoder({
          location: {
            latitude: res.latitude,
            longitude: res.longitude
          },
          success: function (res) {
            var selectedCity = {
              name: res.result.address_component.city,
              pro: res.result.address_component.province
            }
            that.globalData.selectedCity = selectedCity
            wx.setStorage({
              key: 'location',
              data: {
                lat: res.result.location.lat,
                lng: res.result.location.lng,
              },
            })
            home.setData({
              selectedCity: res.result.address_component
            })
            wx.setStorageSync('selectedCity', res.result.address_component)
          }
        });
      },
    })
  },
  getLocalTime: function (nS, len) {
    // 时间戳处理
    // donghong 2017-8-21
    // len == 10 返回日期
    // len == 20 返回时间
    var _date = new Date(parseInt(nS) * 1000).toLocaleString().substr(0, len).split('/')
    if (_date[1].length == 1) {
      _date[1] = '0' + _date[1]
    }
    if (_date[2].length == 3) {
      _date[2] = _date[2].substr(0, 2)
    }
    var date = _date.join('-')
    return date
  },
  testPhone: function (phonenum){
    if (!phonenum||phonenum == '') {
      wx.showModal({
        title: '提示',
        content: '手机号不能为空',
        showCancel: false,
      })

      return false
    } else if (phonenum.length < 11 || !(/^1[34578]\d{9}$/.test(phonenum))) {
      wx.showModal({
        title: '提示',
        content: '请输入正确的手机号码',
        showCancel: false,
      })
      return false
    } else {
        return true
    }

  },
  globalData: {
    userInfo: null,
    appid: 'wx9c6f4e267c270b79',
    // appid: 'wxd678efh567hg6787',
    // url:'http://211.159.171.242/maitian/Api/',
    // imgurl: 'http://211.159.171.242/',
    url: 'https://www.hohoo.tech/index.php/Api/',
    imgurl: 'https://www.hohoo.tech',

  }
})